#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=600000, maxq=300000, maxk=19, inf=0x7ffffff;

int n, m=1, ind[maxn+1], to[2*maxn+1], link[2*maxn+1], val[2*maxn+1], up[maxn+1][maxk+1], dep[maxn+1], nval[maxn+1], ups[maxn+1], sbook[maxn+1], que[maxn+1][2];
bool mark[maxn+1];

void addSide(int u, int v, int w=2) {
    m++;
    to[m] = v;
    val[m] = w;
    link[m] = ind[u];
    ind[u] = m;
}

void getBfs(int que[][2]) {
    int t=1;
    for (int i=1; i<=n; i++) que[i][0]=que[i][1] = 0;
    que[1][0] = 1;
    que[1][1] = 0;
    for (int i=1; i<=n; i++) {
        int o=que[i][0], fa=que[i][1];
        for (int j=ind[o]; j; j=link[j]) {
            if (!sbook[j] && to[j]!=fa) {
                que[++t][0] = to[j];
                que[t][1] = o;
            }
        }
    }
}

void build() {
    getBfs(que);
    for (int t=1; t<=n; t++) {
        int o=que[t][0], fa=que[t][1];
        dep[o] = dep[fa]+1;
        for (int i=1; i<=maxk; i++) {
            up[o][i] = up[up[o][i-1]][i-1];
        }

        for (int i=ind[o]; i; i=link[i]) {
            if (to[i]!=fa) {
                ups[to[i]] = i;
                up[to[i]][0] = o;
            }
        }
    }
}

int dis(int u, int v) {
    int ret;
    if (dep[u]<dep[v]) ret=u, u=v, v=ret;
    ret = 0;
    for (int i=maxk; i>=0; i--) {
        if (dep[up[u][i]]>=dep[v]) {
            ret+=(1<<i)*2;
            u = up[u][i];
        }
    }
    if (u==v) return ret;
    for (int i=maxk; i>=0; i--) {
        if (up[u][i]!=up[v][i]) {
            ret+=(1<<i)*4;
            u=up[u][i], v=up[v][i];
        }
    }
    return ret+4;
}

bool tryDoMid(int x, int xl, int len) {
    if (xl==len/2) {
        nval[x] = min(len/2, nval[x]);
        mark[x] = true;
        return true;
    } else if (xl==len/2-1) {
        if (!sbook[ups[x]]) {
            sbook[ups[x]]=++n;
            sbook[ups[x]^1]=n;
            addSide(x, n, 1);
            addSide(n, x, 1);
            addSide(up[x][0], n, 1);
            addSide(n, up[x][0], 1);
        }
        nval[sbook[ups[x]]] = min(nval[sbook[ups[x]]], len/2);
        mark[sbook[ups[x]]] = true;
        return true;
    }
    return false;
}

void getMid(int u, int v, int len) {
    if (dep[u]<dep[v]) {
        int temp;
        temp=u, u=v, v=temp;
    }
    int x=u, y=v, xl=0, yl=0;

    for (int i=maxk; i>=0; i--) {
        if (dep[up[x][i]]>=dep[v] && xl+(1<<i)*2<=len/2) {
            xl += (1<<i)*2;
            x = up[x][i];
        }
    }

    for (int i=maxk; i>=0; i--) {
        if (up[x][i]!=up[y][i] && xl+(1<<i)*2<=len/2 && yl+(1<<i)*2<=len/2) {
            xl+=(1<<i)*2, yl+=(1<<i)*2;
            x=up[x][i], y=up[y][i];
        }
    }

    if (tryDoMid(x, xl, len)) return;
    else if (tryDoMid(y, yl, len)) return;
    else if (tryDoMid(up[x][0], xl+2, len)) return;
}

void solve() {
    getBfs(que);
    for (int j=n; j>0; j--) {
        int o=que[j][0];
        for (int i=ind[o]; i; i=link[i]) {
            nval[o] = min(nval[o], nval[to[i]]+val[i]);
        }
    }
    for (int j=1; j<=n; j++) {
        int o=que[j][0];
        for (int i=ind[o]; i; i=link[i]) {
            nval[to[i]] = min(nval[to[i]], nval[o]+val[i]);
        }
    }
}

int main() {
    freopen("stone.in", "r", stdin);
    freopen("stone.out", "w", stdout);

    static int que[maxq+1][2];
    int q, tn;
    scanf("%d", &n);
    tn = n;
    for (int i=2; i<=n; i++) {
        int f;
        scanf("%d", &f);
        addSide(f, i);
        addSide(i, f);
    }
    build();
    memset(nval, 127, sizeof nval);
    scanf("%d", &q);
    for (int i=1; i<=q; i++) {
        int size, t1, t2, v=0;
        scanf("%d %d", &size, &t1);
        t2 = t1;
        for (int i=2; i<=size; i++) {
            int t;
            scanf("%d", &t);
            int u1=dis(t1, t), u2=dis(t2, t);
            if (u1>=v && u1>=u2) {
                t2=t;
                v = u1;
            } else if (u2>=v && u2>=u1) {
                t1=t;
                v = u2;
            }
        }
        getMid(t1, t2, v);
    }

    solve();
    for (int i=1; i<=tn; i++) printf("%d\n", nval[i]/2);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
